package com.vnp.cooking;

import java.util.ArrayList;

import com.vnp.cooking.adapter.DetailDacDiemAdapter;
import com.vnp.cooking.bean.ObjAttribute;
import com.vnp.cooking.utils.PreferenceHelper;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class DetailDacdiemActivity extends Activity implements OnClickListener,OnItemClickListener{
	
	ImageButton btnBack;
	ListView listDetail;
	DetailDacDiemAdapter adapter;
	ArrayList<ObjAttribute> listDetails = new ArrayList<ObjAttribute>();
	String title;
	TextView titleDetail;
	PreferenceHelper preferenceHelper;
	int type;
	
	
	public final static int THOIGIANNAU = 0x0;
	public final static int LOAIBUAAN = 0x1;
	public final static int LOAIMONAN = 0x2;
	public final static int NGUONGOC = 0x3;
	public final static int PHUONGPHAP = 0x4;
	public final static int DOKHO = 0x5;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_dacdiem);
		
		preferenceHelper = new PreferenceHelper(this);
		
		listDetails = getIntent().getParcelableArrayListExtra("listDetail");
		for (int i = 0; i < listDetails.size(); i++) {
			Log.i("Test", "ID nhan dc: "+listDetails.get(i).getId());
		}
		title = getIntent().getStringExtra("title");
		type = getIntent().getIntExtra("type", 0);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		listDetail = (ListView) findViewById(R.id.list_detai_dd);
		titleDetail = (TextView) findViewById(R.id.txt_detail_dacdiem_title);
		adapter = new DetailDacDiemAdapter(this, listDetails);
		
		init();
	}
	
	
	void init() {
		titleDetail.setText(title);
		btnBack.setOnClickListener(this);
		listDetail.setOnItemClickListener(this);
		listDetail.setAdapter(adapter);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View item, int position, long id) {
		Log.i("DATA", "DACDIEM ID: "+listDetails.get(position).getId()+"Position: "+position);
		if (type == 1) {
			preferenceHelper.setIdThoigian(listDetails.get(position).getId());
			preferenceHelper.setIdDacdiem(listDetails.get(position).getId());
		} else if (type == 2) {
			preferenceHelper.setIdDacdiem(listDetails.get(position).getId());
			preferenceHelper.setIdLoaibua(listDetails.get(position).getId());
		} else if (type == 3) {
			preferenceHelper.setIdDacdiem(listDetails.get(position).getId());
			preferenceHelper.setIdLoaimonan(listDetails.get(position).getId());
		} else if (type == 4) {
			preferenceHelper.setIdDacdiem(listDetails.get(position).getId());
			preferenceHelper.setIdNguongoc(listDetails.get(position).getId());
		} else if (type == 5) {
			preferenceHelper.setIdDacdiem(listDetails.get(position).getId());
			preferenceHelper.setIdPhuongphap(listDetails.get(position).getId());
		} else if (type == 6) {
			preferenceHelper.setIdDacdiem(listDetails.get(position).getId());
			preferenceHelper.setIdDokho(listDetails.get(position).getId());
		} 
		
		adapter.notifyDataSetChanged();
		
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			closeActivity();
			// truyen thoong tin da chon ve trang truowc tai day
			break;

		default:
			break;
		}
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		closeActivity();
	}
	

}
